package com.real.fact.web.product.application;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

import com.globant.katari.core.application.ValidatableCommand;
import com.real.fact.web.product.domain.Product;
import com.real.fact.web.product.domain.ProductRepository;

public class SaveProductCommand implements ValidatableCommand<Void> {

	private final ProductRepository productRepository;

	private long productId;

	private String name;

	private String description;

	private String category;

	private String brand;

	private float rawPrice;

	private float stock;

	private String code;

	private float deskPrice;

	private String mesaureType;

	private String showDescription;

	public SaveProductCommand(final ProductRepository productRepository) {
		this.productRepository = productRepository;
	}

	public void init() {
		if (productId > 0) {
			Product product = productRepository.findById(productId);
			name = product.getName();
			description = product.getDescription();
			category = product.getCategory();
			brand = product.getBrand();
			rawPrice = product.getRawPrice();
			stock = product.getStock();
			deskPrice = product.getDeskPrice();
			mesaureType = product.getMeasureType();
			showDescription = product.getShowDescription();
		}
	}

	public Void execute() {
		Product product = null;
		if (productId > 0) {
			product = productRepository.findById(productId);
			product.modify(name, code, description, category, brand, rawPrice,
					deskPrice, stock, mesaureType, showDescription);
		} else {
			product = new Product(name, code, description, category, brand,
					rawPrice, deskPrice, stock, mesaureType, showDescription);
		}
		productRepository.save(product);
		return null;
	}

	public String getCode() {
		return code;
	}

	public float getDeskPrice() {
		return deskPrice;
	}

	public String getMesaureType() {
		return mesaureType;
	}

	public String getShowDescription() {
		return showDescription;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public float getRawPrice() {
		return rawPrice;
	}

	public void setRawPrice(float rawPrice) {
		this.rawPrice = rawPrice;
	}

	public float getStock() {
		return stock;
	}

	public void setStock(float stock) {
		this.stock = stock;
	}

	public void validate(Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
				"Name is empty");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "category",
				"Category is empty");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "brand",
				"Brand is empty");
	}

	public long getProductId() {
		return productId;
	}

	public void setProductId(long productId) {
		this.productId = productId;
	}

  public void setCode(String code) {
    this.code = code;
  }

  public void setDeskPrice(float deskPrice) {
    this.deskPrice = deskPrice;
  }

  public void setMesaureType(String mesaureType) {
    this.mesaureType = mesaureType;
  }

  public void setShowDescription(String showDescription) {
    this.showDescription = showDescription;
  }

}
